from django.core.context_processors import csrf
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.shortcuts import render_to_response
from computers.models import Comp, fix_com

#use Template(main.html) for main homepage
def home(request):
    return render_to_response('main.html')

#use Template(input_com.html) for Input Broken Computer
def contact(request):
    return render_to_response('input_com.html', locals())

#use Template(input_fix.html) for Input Fixer & Date of Repair
def contact2(request):
    return render_to_response('input_fix.html', locals())

#for click "Show Data" in main page
#def table(request):
#    return render_to_response('show_com.html')

#use Template(create_com.html) for Create New Computer
#def contact3(request):
#    return render_to_response('create_com.html', locals())

#------------------------------------ Function show ---------------------------------------
#------------------------------------------------------------------------------------------
#This function use from Template 'input_com.html' Submit for operate about Broken Computer.
#------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------
def show(request):

    input_id = request.GET['id']
    input_effect = request.GET['effect']

    #if 'id' and 'effect' in request.GET:
    Comp.objects.filter(com_id = input_id).update(status_com = 'Bad', brok_occur = input_effect)
    value_table = Comp.objects.all()

    #check good computer
    check = Comp.objects.filter(status_com = "Good")
    i = 0
    for i in range(len(check)):
        i = i+1
            
    good_data = i
    i = 0
    
    #check bad computer
    check = Comp.objects.filter(status_com = "Bad")
    for i in range(len(check)):
        i = i+1

    bad_data = i
    i = 0

    total = good_data + bad_data        #all Computer

    return render_to_response('show_com.html', locals())

#----------------------------------- Function repair -----------------------------------------
#---------------------------------------------------------------------------------------------
#This function use from Template 'input_fix.html' Submit for operate about Repairing Computer.
#---------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------
def repair(request):
    input_id = request.GET['idss']
    input_man = request.GET['man']
    input_day = request.GET['day']

    #if 'idss' and 'man' and 'day' in request.GET:
    Comp.objects.filter(com_id = input_id).update(status_com = 'Good', brok_occur = 'None')
    fix_com.objects.create(com_id_fix = input_id, date_fix = input_day, fixer = input_man)
    fix_table = fix_com.objects.all()

    return render_to_response('show_fix.html', locals())

#----------------------------------- Function create -------------------------------------------
#-----------------------------------------------------------------------------------------------
#This function use from Template 'create_com.html' Submit for operate about Create new Computer.
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
#def create(request):
#    cre_id = request.GET['idc']
#    cre_status = request.GET['status']
#    cre_occur = request.GET['occur']
#
#    #if 'idc' and 'status' and 'occur' in request.GET:
#    Comp.objects.create(com_id = cre_id, status_com = cre_status, brok_occur = cre_occur)
#    cre_com = Comp.objects.all()
#
#    check = Comp.objects.filter(status_com = "Good")
#    i = 0
#    for i in range(len(check)):
#        i = i+1
#            
#    good_data = i
#    i = 0
#
#    check = Comp.objects.filter(status_com = "Bad")
#    for i in range(len(check)):
#        i = i+1
#
#    bad_data = i
#    i = 0
#
#    total = good_data + bad_data
#
#    return render_to_response('show_create.html', locals())
  



